<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./art-template(1).js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #e1e1e1;

        }

        ul {
            background-color: #fff;

            width: 1000px;
            margin: auto;
        }

        li {
            height: 60px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #dedede;
            position: relative;
        }

        li:hover {
            background-color: #f6f6f6;
        }

        li img {
            width: 35px;
            height: 35px;
            display: block;
            margin: 0 10px;
            cursor: pointer;
        }

        li p {
            color: #333;
        }

        li p>span:first-child {
            color: #9e78c0;
            font-size: 14px;
        }

        li p>span:last-child {
            font-size: 10px;
            color: #b4b4b4;
        }

        li>span {
            display: block;
            width: 40px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            border-radius: 5px;
            font-size: 12px;
            margin: 0 10px;
            background-color: #e5e5e5;
            color: #999;
        }

        li>span.top {
            background: #80bd01;
            color: #fff;
            font-size: 12px;
        }

        h3 {
            font-weight: 400px;
        }

        h3>a {
            color: #333;
            font-size: 16px;
            text-decoration: none;
        }

        h3>a:hover {
            text-decoration: underline;
        }

        time {
            cursor: pointer;
            color: #778087;
            font-size: 11px;
            position: absolute;
            right: 10px;
            top: 20px
        }

        .nav {
            padding: 10px;
            background-color: #f6f6f6;
            border-radius: 3px 3px 0 0;
            line-height: 20px;
            width: 980px;
            height: 20px;
            margin: auto;
        }

        .nav a {

            color: #80bd01;
            padding: 3px 4px;
            border-radius: 3px;
            margin: 0 10px;
            font-size: 14px;
            text-decoration: none;
            transition: 0.02s linear;

        }

        .nav a:hover {
            color: #08c;
        }

        .nav .jk {
            background: #80bd01;
            color: #fff;
        }

        ol {
            display: flex;
            margin-bottom: 100px;
            height: 40px;
            margin: 10px 0 0 10px;
            width: 1000px;
            background-color: #fff;
            border-radius: 0 0 5px 5px;
            margin: auto;
            padding: 0;

        }

        ol>li {
            width: 8px;
            height: 30px;
            text-align: center;
            float: left;
            padding: 4px 12px;
            line-height: 20px;
            text-decoration: none;
            background-color: #fff;
            border: 1px solid #ddd;
            border-left-width: 0;

        }

        ol>li:hover {
            color: #80bd01;
        }
    </style>
</head>

<body>
    <div class="box">
    </div>
    <script id="con" type="text/html">
     <ul>
        {{each data as item}}
<li>
    <img src={{item.author.avatar_url}} alt="">
    <p>
        <span>{{item.reply_count}}</span>
        /
        <span>{{item.visit_count}}</span>
    </p>
    {{if item.top}}
    <span class="top">置顶</span>
    {{else if item.good}}
    <span class="top">精华</span>
    {{else if item.tab == "share"}}
    <span>分享</span>
    {{else}}
    <span>问答</span>
    {{/if}}
    <h3><a href="">{{item.title}}</a></h3>

    <time>{{item.last_reply_at | time}}</time>
</li>
        {{/each}}
     </ul>
    </script>
    <script>
        template.helper("time",function(lastTime){
            var time1 = new Date().getTime(); //获取当前时间的时间戳
            var time2 = new Date(lastTime).getTime(); //获取参数传递的时间的时间戳
            var cha = time1 - time2; //得到时间戳的差
            var y = parseInt(cha / 1000 / 60 / 60 / 24 / 30 / 12);
            var m = parseInt(cha / 1000 / 60 / 60 / 24 / 30 % 12);
            var d = parseInt(cha / 1000 / 60 / 60 / 24 % 30);
            var h = parseInt(cha / 1000 / 60 / 60 % 24);
            var mi = parseInt(cha / 1000 / 60 % 60);
            if (y) {
                return y + "年前"
            } else if (m) {
                return m + "月前"
            } else if (d) {
                return d + "天前"
            } else if (h) {
                return h + "小时前"
            } else if (mi) {
                return mi + "分钟前"
            } else {
                return "刚刚";
            }
        })
        
        fetch("https://cnodejs.org/api/v1/topics").then((data) => {
            return data.json();
        }).then(function (res) {
            console.log(res.data[0]);
            var str = template("con", res);
            document.querySelector(".box").innerHTML = str;
        })
    </script>
</body>

</html>